package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import Utils.ObjectResult;

import com.github.pagehelper.PageInfo;
import com.taotao.pojo.TbContent;
import com.taotao.service.TbContentService;

@RequestMapping(value="content")
@Controller
public class ContentController {

	@Autowired
	TbContentService service;
	
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> saveEntity(TbContent content){
		try {
			service.save(content);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<ObjectResult> queryEntity(@RequestParam("categoryId")Long categoryId,
			                                @RequestParam(value="page", defaultValue="1")Integer page,
			                                @RequestParam(value="rows", defaultValue="10")Integer rows){
		try {
			PageInfo<TbContent> pageInfo = service.queryEntities(categoryId, page, rows);
			return ResponseEntity.ok(new ObjectResult(pageInfo.getTotal(),pageInfo.getList()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
}
